//
//  AddressBookTableViewCell.m
//  SouthChina
//
//  Created by Mr.Y on 14/11/3.
//  Copyright (c) 2014年 科创. All rights reserved.
//


#define checkBoxWH 20
#define commgap 5


#import "AddressBookTableViewCell.h"

@implementation AddressBookTableViewCell

@synthesize contact,isCreatrGroup;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self) {
        
        _headImage = [[UIImageView alloc] initWithFrame:CGRectZero];
        
        _titleLabel = [[UILabel alloc]initWithFrame:CGRectZero];
        
        _detailLabel = [[UILabel alloc]initWithFrame:CGRectZero];
        
        _checkBox = [[UIImageView alloc] init];
        
        _allcontentView =[[UIView alloc]initWithFrame:CGRectZero];

        _bottomLine =[[UIView alloc]initWithFrame:CGRectZero];

        
        [_titleLabel setFont:[UIFont systemFontOfSize:14]];
        [_detailLabel setFont:[UIFont systemFontOfSize:13]];
        
        _detailLabel.textColor = BASIC_GRAYLINE_COLOR;
        
        [self addSubview:_allcontentView];
        [_allcontentView addSubview:_headImage];
        [_allcontentView addSubview:_titleLabel];
        [_allcontentView addSubview:_detailLabel];
        [_allcontentView addSubview:_checkBox];
        [_allcontentView addSubview:_bottomLine];
        [_bottomLine setBackgroundColor:BASIC_GRAYLINE_COLOR];

        [self setBackgroundColor:[UIColor clearColor]];
        
    }
    return self;
}


- (void)layoutSubviews{
   
    self.allcontentView.frame =  CGRectMake(0, 0, SCREEN_WIDTH, CONTACT_HEIGHT);
    if (isCreatrGroup) {
        self.checkBox.frame = CGRectMake(commgap, (CONTACT_HEIGHT-checkBoxWH)/2,  checkBoxWH, checkBoxWH);
        self.checkBox.backgroundColor = [UIColor whiteColor];
    }
    self.headImage.frame = CGRectMake(isCreatrGroup?(2*commgap+checkBoxWH):commgap, 5, 40, 40);
    self.headImage.layer.masksToBounds = YES;
    self.headImage.layer.cornerRadius = 8.0;
    self.headImage.contentMode = UIViewContentModeScaleToFill;

    int titleWidth= isCreatrGroup?(SCREEN_WIDTH-5*commgap-checkBoxWH - 40)/2:(SCREEN_WIDTH-4*commgap-40)/2;
    
    self.titleLabel.frame = CGRectMake(isCreatrGroup?(3*commgap+checkBoxWH+40):(2*commgap+40), 5, titleWidth, 40);
    
    self.detailLabel.frame = CGRectMake(SCREEN_WIDTH-commgap-titleWidth-15, 5, titleWidth, 40);
    self.detailLabel.textAlignment = NSTextAlignmentRight;
    
    self.checkBox.hidden = !isCreatrGroup;
    

    self.bottomLine.frame =CGRectMake(5, CONTACT_HEIGHT-1, SCREEN_WIDTH-10, 1);
    
    NSURL *url = [NSURL URLWithString: contact.mHEAD_IMG];
    [self.headImage setImageWithURL:url placeholderImage:[UIImage imageNamed:@"empty_photo"]];
    self.titleLabel.text = [NSString stringWithFormat:@"%@",contact.mNAME];
    self.detailLabel.text = contact.mDEPT;


}

-(void) setContactValue:(Contact*)contactt isCreat:(BOOL)isCreat{

    self.isCreatrGroup = isCreat;
    self.contact = contactt;

    
}

-(void)setContactSelected:(BOOL)isSelected
{
    
    [self.checkBox setImage:[UIImage imageNamed:isSelected?@"contact_select":@"contact_unselect"]];
    
}


@end
